package com.zdlyh.utils;

import com.zdlyh.constants.SystemConstants;
import com.zdlyh.domain.entity.Article;
import com.zdlyh.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author liuYuhui
 * @Date 2023/12/11 10:26
 */
@Component
public class RunTask {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private RedisCache redisCache;

    public void updateViewCount() {
        // 查询博客信息
        List<Article> articles = articleService.list();
        Map<String, Integer> viewCountMap = articles.stream()
                .collect(Collectors.toMap(article -> article.getId().toString(),
                        article -> article.getViewCount().intValue()
                ));
        // 存储到Redis中 key:article_viewCount  value:map(key:id  value:viewCount)
        for (String string : viewCountMap.keySet()) {
            System.out.println(string+"-----"+viewCountMap.get(string));
        }
        redisCache.setCacheMap(SystemConstants.article_viewCount,viewCountMap);
    }
}
